<?php include_once 'header.php'; ?>
<div class="row">
    <div class="col-md-12">
        <div class="card-deck">
            <div class="card shadow mb-2">
                <div class="card-header">
                    <strong class="card-title">
                        APPID:<? echo $this->app['id']; ?>
                    </strong>
                </div>
                <div class="card-body">
                    <form>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label for="inputEmail4">
                                    应用图标
                                </label>
                                <div class="avatar avatar-lg">
                                    <a href="javascript:upload_logo();">
                                        <img id="img_applogo"
                                             src="/<? echo !empty($this->app['app_logo']) && file_exists($this->app['app_logo']) ? $this->app['app_logo'] : 'assets/images/add.png'; ?>"
                                             class="avatar-img app-logo-add">
                                    </a>
                                    <input style="display: none" id="app_logo" type="file" onchange="showlogo(this);"
                                           accept="image/*" multiple/>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>应用名称</label>
                            <input type="text" class="form-control" id="app_name" placeholder="应用名称"
                                   value="<?= $this->app['app_name'] ?>">
                        </div>
                        <div class="form-group">
                            <label>APPKEY</label>
                            <div class="input-group">
                                <input type="text" class="form-control" id="app_key" placeholder="应用密钥"
                                       value="<?= $this->app['app_key'] ?>" readonly>
                                <div class="input-group-append">
                                    <button class="btn btn-light" type="button" id="alter_appkey">更换</button>
                                    <button class="btn btn-primary" type="button" id="copy_appkey">复制</button>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>运营模式<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                 title="若设置免费时，所有验证卡密是VIP否过期时都返回真"></span></label>
                            <select id="app_mode" class="form-control">
                                <option value="y" <? echo $this->app['app_mode'] == 'y' ? 'selected' : ''; ?>>收费
                                </option>
                                <option value="n" <? echo $this->app['app_mode'] == 'n' ? 'selected' : ''; ?>>免费
                                </option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>应用状态<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                 title="关闭后则所有API禁止请求，同时返回关闭通知内容"></span></label>
                            <select id="app_state" class="form-control" onchange="select_app_state(this.value)">
                                <option value="on" <? echo $this->app['app_state'] == 'on' ? 'selected' : ''; ?>>正常
                                </option>
                                <option value="off" <? echo $this->app['app_state'] == 'off?' ? 'selected' : ''; ?>>
                                    关闭
                                </option>
                            </select>
                        </div>

                        <div class="form-group"
                             id="div_app_off_msg" <? echo $this->app['api_encrypt'] == 'on' ? 'hidden' : ''; ?>>
                            <label>关闭通知</label>
                            <textarea class="form-control" id="app_off_msg" rows="4"
                                      placeholder="应用关闭后的公告"><?= str_replace("\\n", "&#10;", $this->app['app_off_msg']) ?></textarea>
                        </div>

                    </form>
                </div>
            </div>
        </div>

        <div class="card-deck">
            <div class="card shadow mb-2">
                <div class="card-header">
                    <strong class="card-title">
                        数据安全
                    </strong>
                </div>
                <div class="card-body">
                    <form>
                        <div class="row">
                            <div class="col-6">
                                <div class="form-group">
                                    <label>数据签名<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                         title="开启数据签名有利于保护数据安全不被篡改"></span></label>
                                    <select id="api_sign" class="form-control">
                                        <option value="on" <? echo $this->app['api_sign'] == 'on' ? 'selected' : ''; ?>>
                                            开启
                                        </option>
                                        <option value="off" <? echo $this->app['api_sign'] == 'off' ? 'selected' : ''; ?>>
                                            关闭
                                        </option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label>数据有效期<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                           title="设置数据有效期，可验证接收的数据是否有效，避免用户修改本机时间欺骗系统验证"></span></label>
                                    <div class="input-group">
                                        <input type="number" class="form-control" placeholder="建议值为：100"
                                               id="api_data_exp" value="<?= $this->app['api_data_exp']; ?>">
                                        <div class="input-group-append">
                                            <span class="input-group-text">秒</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <div class="form-group">
                                    <label>
                                        数据加密
                                    </label>
                                    <div class="input-group">
                                        <select id="api_encrypt" class="form-control" onchange="select_api_encrypt(this.value)">
                                            <option value="on" <? echo $this->app['api_encrypt'] == 'on' ? 'selected' : ''; ?>>
                                                开启
                                            </option>
                                            <option value="off" <? echo $this->app['api_encrypt'] == 'off' ? 'selected' : ''; ?>>
                                                关闭
                                            </option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label>心跳有效期<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                           title="设置心跳有效期，如果N秒内没有收到客户端心跳，Token失效；0为不开启"></span></label>
                                    <div class="input-group">
                                        <input type="number" class="form-control" placeholder="建议值为：100"
                                               id="api_heartbeat" value="<?= $this->app['api_heartbeat']; ?>">
                                        <div class="input-group-append">
                                            <span class="input-group-text">秒</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group"
                             id="api_encrypt_on" <? echo $this->app['api_encrypt'] == 'off' ? 'hidden' : ''; ?>>
                            <label>RSA公钥
                                <button id='refresh_key' class="fe fe-refresh-cw btn btn-danger btn-sm"
                                        data-toggle="tooltip" title="更换后脚本也要更新，否则不能使用，谨慎更换!!!!"> 更换
                                </button>
                            </label>
                            <textarea disabled class="form-control" id="api_encrypt_public_key"
                                      rows="11"><? $api_key = json_decode($this->app['api_key'], true);
                                echo $this->app['api_encrypt'] == 'on' ? $api_key['public'] : "正在获取中..."; ?></textarea>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="card-deck">
            <div class="card shadow mb-2">
                <div class="card-header">
                    <strong class="card-title">
                        解绑设置
                    </strong>
                </div>
                <div class="card-body">
                    <form>
                        <div class="form-group">
                            <label>允许自解绑设备</label>
                            <div class="input-group">
                                <select id="user_client_untie" class="form-control"
                                        onchange="select_user_client_untie(this.value)">
                                    <option value="y" <? echo $this->app['user_client_untie'] == 'y' ? 'selected' : ''; ?>>
                                        允许
                                    </option>
                                    <option value="n" <? echo $this->app['user_client_untie'] == 'n' ? 'selected' : ''; ?>>
                                        不允许
                                    </option>
                                </select>
                            </div>
                        </div>
                        <div class="row"
                             id="user_client_untie_y" <? echo $this->app['user_client_untie'] == 'n' ? 'hidden' : ''; ?>>
                            <div class="col-6">
                                <div class="form-group">
                                    <label>解绑周期<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                         title="绑定设备的时间和解绑设备的时间不能小于此设置,0则不限制"></span></label>
                                    <div class="input-group">
                                        <input type="number" class="form-control" placeholder="1"
                                               id="user_client_untie_cycle"
                                               value="<?= $this->app['user_client_untie_cycle']; ?>">
                                        <div class="input-group-append">
                                            <span class="input-group-text">秒</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group">
                                    <label>解绑惩罚<span class="fe fe-help-circle ml-1" data-toggle="tooltip"
                                                         title="对于自解绑的卡密进行VIP扣除惩罚，0则不惩罚"></span></label>
                                    <div class="input-group">
                                        <input type="number" class="form-control" placeholder="1"
                                               id="user_client_untie_punish"
                                               value="<?= $this->app['user_client_untie_punish']; ?>">
                                        <div class="input-group-append">
                                            <span class="input-group-text">秒</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>

        <div class="card-deck">
            <div class="card shadow mb-4">
                <div class="card-body">
                    <form>
                        <button type="submit" class="btn btn-primary btn-block" id="submit">提交</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $("#submit").click(function () {
        var submit_data = {};
        submit_data.app_name = $("#app_name").val();
        submit_data.app_key = $("#app_key").val();
        submit_data.app_mode = $("#app_mode").val();
        submit_data.app_state = $("#app_state").val();
        submit_data.app_off_msg = $("#app_off_msg").val().replace(/\n/g, "\\n");
        submit_data.api_sign = $("#api_sign").val();
        submit_data.api_encrypt = $("#api_encrypt").val();
        submit_data.api_data_exp = $("#api_data_exp").val();
        submit_data.api_heartbeat = $("#api_heartbeat").val();
        submit_data.user_client_untie = $("#user_client_untie").val();
        submit_data.user_client_untie_punish = $("#user_client_untie_punish").val();
        submit_data.user_client_untie_cycle = $("#user_client_untie_cycle").val();
        $("#submit").html('<i class="spinner-ui fe fe-rotate-cw fe-12 mr-2"></i>正在提交');
        $("#submit").attr("disabled", true);
        $.ajax({
            cache: false,
            type: "POST",
            data: submit_data,
            dataType: "json",
            success: function (data) {
                $("#submit")
                    .html("提交");
                $("#submit")
                    .attr("disabled", false);
                if (data.code == 200) {
                    cocoMessage.success("更新成功", 2000)
                } else {
                    cocoMessage.error(data.msg, 2000)
                }
            }
        });
        return false
    });
    $("#refresh_key").click(function () {
        get_encrypt_key()
        return false
    })

    function select_app_state(i) {
        if (i == "on") {
            $("#div_app_off_msg")
                .attr("hidden", true)
        } else {
            $("#div_app_off_msg")
                .attr("hidden", false)
        }
    }

    function select_user_client_untie(i) {
        if (i == "n") {
            $("#user_client_untie_y")
                .attr("hidden", true)
        } else {
            $("#user_client_untie_y")
                .attr("hidden", false)
        }
    }

    function get_encrypt_key() {
        let submit_data = {}
        submit_data.act = 'get_encrypt_key'
        submit_data.app_name = $("#app_name").val();
        $.ajax({
            url: 'rsa_key',
            cache: false,
            type: "POST",
            data: submit_data,
            dataType: "json",
            success: function (data) {
                $("#api_encrypt_public_key").val(data.data.public)
            }
        });
    }

    function select_api_encrypt(i) {
        if (i == "off") {
            $("#api_encrypt_on").attr("hidden", true)
        } else {
            $("#api_encrypt_on").attr("hidden", false)
            if ($("#api_encrypt_public_key").val().length <= 10) {
                get_encrypt_key()
            }

        }
    }

    $("#copy_appkey")
        .click(function () {
            $("#app_key")
                .select();
            document.execCommand("copy");
            cocoMessage.success("复制成功", 2000)
        });
    $("#alter_appkey")
        .click(function () {
            var appkey = randomString(32);
            $("#app_key").val(appkey)
        });
</script>
<?php include_once 'footer.php'; ?>
